package ms.unicorn.diagram.view;

import ms.unicorn.diagram.part.UnicornDiagramEditorPlugin;
import ms.unicorn.util.ViewUtil;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.ui.preferences.IPreferenceConstants;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.notation.FontStyle;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.impl.ShapeImpl;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;

public class ShapeEx extends ShapeImpl implements ViewEx {

	private EditPart editpart;
	private String name;

	public ShapeEx(EObject entity, PreferencesHint preferencesHint) {
		//node.setDefinition(viewDef);
		ShapeEx node = this;
		node.setLayoutConstraint(NotationFactory.eINSTANCE.createBounds());
				
		//node.setType("ms.unicorn.Entity");
		
		node.setElement(entity);
		//ViewUtil.stampShortcut(containerView, node);
		
		// initializeFromPreferences 
		if (preferencesHint == null)
			preferencesHint = UnicornDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT;
		
		final IPreferenceStore prefStore = (IPreferenceStore) preferencesHint.getPreferenceStore();
		
		
		RGB lineRGB = PreferenceConverter.getColor(prefStore, IPreferenceConstants.PREF_LINE_COLOR);
		ViewUtil.setStructuralFeatureValue(node,
				NotationPackage.eINSTANCE.getLineStyle_LineColor(),
				FigureUtilities.RGBToInteger(lineRGB));
		FontStyle nodeFontStyle = (FontStyle) node
				.getStyle(NotationPackage.Literals.FONT_STYLE);
		if (nodeFontStyle != null) {
			FontData fontData = PreferenceConverter.getFontData(prefStore, IPreferenceConstants.PREF_DEFAULT_FONT);
			nodeFontStyle.setFontName(fontData.getName());
			nodeFontStyle.setFontHeight(fontData.getHeight());
			nodeFontStyle.setBold((fontData.getStyle() & SWT.BOLD) != 0);
			nodeFontStyle.setItalic((fontData.getStyle() & SWT.ITALIC) != 0);
			RGB fontRGB = PreferenceConverter.getColor(prefStore, IPreferenceConstants.PREF_FONT_COLOR);
			nodeFontStyle.setFontColor(FigureUtilities.RGBToInteger(fontRGB).intValue());
		}
		RGB fillRGB = PreferenceConverter.getColor(
				prefStore, IPreferenceConstants.PREF_FILL_COLOR);
		ViewUtil.setStructuralFeatureValue(node,
				NotationPackage.eINSTANCE.getFillStyle_FillColor(),
				FigureUtilities.RGBToInteger(fillRGB));
	}

	@Override
	public EditPart getEditPart() {
		return this.editpart;
	}

	@Override
	public void setEditPart(EditPart part) {
		editpart = part;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

}
